{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "464a81c5f1ab395a",
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "source": [
    "学习目标\n",
    "- 说明数组的属性，形状、类型"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "236cd964e4663f81",
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "source": [
    "# 1 ndarray的属性\n",
    "\n",
    "| 属性名字           | 属性解释           | \n",
    "|------------------|--------------|\n",
    "| ndarray.shape\t   |数组维度的元组|\n",
    "| ndarray.ndim\t    |数组维数|\n",
    "| ndarray.size\t    |数组中的元素数量|\n",
    "| ndarray.itemsize\t |一个数组元素的长度（字节）|\n",
    "| ndarray.dtype\t   |数组元素的类型|"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "bf35b79d6ce0dcec",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-02-20T08:57:10.348966200Z",
     "start_time": "2024-02-20T08:57:10.328016100Z"
    },
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(2, 3)\n",
      "2\n",
      "6\n",
      "4\n",
      "int32\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "ndarray = np.array([[1, 2, 3], [4, 5, 6]])\n",
    "print(ndarray.shape)\n",
    "print(ndarray.ndim)\n",
    "print(ndarray.size)\n",
    "print(ndarray.itemsize)  # item的类型为int32 32=4*8，4子节\n",
    "print(ndarray.dtype)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "92810539a318ea7a",
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "source": [
    "# 2 ndarray的形状"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "5a85f68eb44baad0",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-02-20T08:57:10.390241300Z",
     "start_time": "2024-02-20T08:57:10.351998500Z"
    },
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(2, 3)\n",
      "(4,)\n",
      "(2, 2, 3)\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "# 创建不同形状的数组\n",
    "a = np.array([[1, 2, 3], [4, 5, 6]])\n",
    "b = np.array([1, 2, 3, 4])\n",
    "c = np.array([[[1, 2, 3], [4, 5, 6]], [[1, 2, 3], [4, 5, 6]]])\n",
    "print(a.shape)\n",
    "print(b.shape)\n",
    "print(c.shape)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "301f8a98bd2b073d",
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "source": [
    "# 3 ndarray的类型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "29409f0668b8970a",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-02-20T08:57:10.391213300Z",
     "start_time": "2024-02-20T08:57:10.365429100Z"
    },
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "numpy.dtypes.Int32DType"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "source = np.array([[1, 2, 3], [4, 5, 6]])\n",
    "type(source.dtype)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "49fb7d0a2fb0a24f",
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "source": [
    "## ndarray的类型\n",
    "\n",
    "| 名称          | 描述                                              | 简写  |\n",
    "| ------------- | ------------------------------------------------- | ----- |\n",
    "| np.bool       | 用一个字节存储的布尔类型（True或False）           | 'b'   |\n",
    "| np.int8       | 一个字节大小，-128 至 127                         | 'i'   |\n",
    "| np.int16      | 整数，-32768 至 32767                             | 'i2'  |\n",
    "| np.int32      | 整数，-2^31 至 2^32 -1                            | 'i4'  |\n",
    "| np.int64      | 整数，-2^63 至 2^63 - 1                           | 'i8'  |\n",
    "| np.uint8      | 无符号整数，0 至 255                              | 'u'   |\n",
    "| np.uint16     | 无符号整数，0 至 65535                            | 'u2'  |\n",
    "| np.uint32     | 无符号整数，0 至 2^32 - 1                         | 'u4'  |\n",
    "| np.uint64     | 无符号整数，0 至 2^64 - 1                         | 'u8'  |\n",
    "| np.float16    | 半精度浮点数：16位，正负号1位，指数5位，精度10位  | 'f2'  |\n",
    "| np.float32    | 单精度浮点数：32位，正负号1位，指数8位，精度23位  | 'f4'  |\n",
    "| np.float64    | 双精度浮点数：64位，正负号1位，指数11位，精度52位 | 'f8'  |\n",
    "| np.complex64  | 复数，分别用两个32位浮点数表示实部和虚部          | 'c8'  |\n",
    "| np.complex128 | 复数，分别用两个64位浮点数表示实部和虚部          | 'c16' |\n",
    "| np.object_    | python对象                                        | 'O'   |\n",
    "| np.string_    | 字符串                                            | 'S'   |\n",
    "| np.unicode_   | unicode类型                                       | 'U'   |"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "681436d9ad0515a6",
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "source": [
    "## 创建数组的时候指定类型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "10232f662b619f95",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-02-20T08:57:10.436106500Z",
     "start_time": "2024-02-20T08:57:10.394215Z"
    },
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dtype('float32')"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.array([[1, 2, 3],[4, 5, 6]], dtype=np.float32)\n",
    "a.dtype"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "67a83a3e08c1d899",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-02-20T08:57:10.461337400Z",
     "start_time": "2024-02-20T08:57:10.439106900Z"
    },
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([b'python', b'tensorflow', b'scikit-learn', b'numpy'], dtype='|S12')"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr = np.array(['python', 'tensorflow', 'scikit-learn', 'numpy'], dtype = np.string_)\n",
    "arr"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3c90c38832027f2c",
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "source": [
    "# 4 总结\n",
    "数组的基本属性【知道】\n",
    "\n",
    "| 属性名字           | 属性解释           | \n",
    "|------------------|--------------|\n",
    "| ndarray.shape\t   |数组维度的元组|\n",
    "| ndarray.ndim\t    |数组维数|\n",
    "| ndarray.size\t    |数组中的元素数量|\n",
    "| ndarray.itemsize\t |一个数组元素的长度（字节）|\n",
    "| ndarray.dtype\t   |数组元素的类型|"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
